//
//  HXApplyViewController.m
//  KD
//
//  Created by ifly on 16/3/14.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXApplyViewController.h"
#import "HXApplyListGroupModel.h"
#import "HXApplyItemModel.h"
#import "HXReachTableViewController.h"
#import "HXJobRecordTableViewController.h"
#import "HXFileTableViewController.h"
#import "HXCeaseTableViewController.h"
#import "HXMyChatViewController.h"
#import "HXQiYeLocaleJobTableViewController.h"//企业检查
#import "HXIflyJobRecordTableViewController.h"
#import "HXCompanyNotifyTableViewController.h"

@interface HXApplyViewController ()
@property(nonatomic,strong) NSMutableArray *data;
@property(nonatomic,strong) NSMutableArray *corpData;
@property(nonatomic,strong) NSMutableArray *manageData;
@property(nonatomic,strong) NSMutableArray *companyData;
@end

@implementation HXApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ApplyTableViewCell"];
    [self userLogin];
    self.tableView.tableFooterView = [[UIView alloc]init];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogin) name:nUserLogin object:nil];//监听用户登录操作
}
//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

#pragma mark - 监听用户登录的通知方法 主管和企业的该界面不同
-(void)userLogin{
    if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeManage) {//是主管
        self.data = self.manageData;
    }else if([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeCorp){//是企业
        if ([[UserDefaultHandler getUserDetault:Users_IsPayVip]integerValue]){
            self.data = self.companyData;
        }else {
            self.data = self.corpData;
        }
    }else if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeBank){
        self.data = self.corpData;
    }
    else{
        self.data = nil;
    }
    [self.tableView reloadData];
}

#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    HXApplyListGroupModel *group = self.data[section];
    return group.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplyTableViewCell" forIndexPath:indexPath];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ApplyTableViewCell"];
    HXApplyListGroupModel *group = self.data[indexPath.section];
    HXApplyItemModel *item = group.items[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:item.icon];
    cell.textLabel.text = item.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    HXApplyListGroupModel *group = self.data[section];
    return group.headerTitle;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HXApplyListGroupModel *group = self.data[indexPath.section];
    HXApplyItemModel *item = group.items[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([GlobalUtil isNilValue:item.destVc])return;
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Apply" bundle:nil];
    UIViewController *meVc = [sb instantiateViewControllerWithIdentifier:item.destVc];
    meVc.title = item.title;
    [self.navigationController pushViewController:meVc animated:YES];
}
#pragma mark - 懒加载
- (NSMutableArray *)corpData{
    if (_corpData == nil) {
        _corpData = [[NSMutableArray alloc]init];
        HXApplyItemModel *item1 = [[HXApplyItemModel alloc]initWithIcon:@"apply_1" title:@"公文通知" destVc:@"HXFileTableViewController"];
        HXApplyItemModel *item2 = [[HXApplyItemModel alloc]initWithIcon:@"apply_2" title:@"工地现场" destVc:@"HXGISViewController"];
        //HXApplyItemModel *item3 = [[HXApplyItemModel alloc]initWithIcon:@"apply_5" title:@"工作汇报" destVc:@"HXIflyJobRecordTableViewController"];
        HXApplyListGroupModel *group3 = [[HXApplyListGroupModel alloc]init];
        group3.items = @[item1,item2];
        [_corpData addObject:group3];
    }
    return _corpData;
}
-(NSMutableArray *)companyData {
    if (_companyData == nil) {
        _companyData = [[NSMutableArray alloc] init];
        HXApplyItemModel *item3 = [[HXApplyItemModel alloc]initWithIcon:@"gz" title:@"工作汇报" destVc:@"HXIflyJobRecordTableViewController"];
        HXApplyItemModel * item4 = [[HXApplyItemModel alloc] initWithIcon:@"apply_1" title:@"通知" destVc:@"HXCompanyNotifyTableViewController"];
        HXApplyListGroupModel *group3 = [[HXApplyListGroupModel alloc]init];
        group3.items = @[item4,item3];
//        group3.items = @[item3];
        [_companyData addObject:group3];
    }
    return _companyData;
}


- (NSMutableArray *)manageData{
    if (_manageData == nil) {
        _manageData = [[NSMutableArray alloc]init];
        HXApplyItemModel *item1 = [[HXApplyItemModel alloc]initWithIcon:@"apply_1" title:@"公文通知" destVc:@"HXFileTableViewController"];
        HXApplyItemModel *item2 = [[HXApplyItemModel alloc]initWithIcon:@"apply_2" title:@"工地现场" destVc:@"HXGISViewController"];
        HXApplyItemModel *item3 = [[HXApplyItemModel alloc]initWithIcon:@"apply_3" title:@"工地检查" destVc:@"HXLocaleJobTableViewController"];
        HXApplyItemModel *item8 = [[HXApplyItemModel alloc]initWithIcon:@"qyjc" title:@"企业检查" destVc:@"HXQiYeLocaleJobTableViewController"];
        HXApplyItemModel *item4 = [[HXApplyItemModel alloc]initWithIcon:@"apply_4" title:@"停工与整改项目" destVc:@"HXCeaseTableViewController"];
        HXApplyItemModel *item5 = [[HXApplyItemModel alloc]initWithIcon:@"apply_5" title:@"工作周报" destVc:@"HXJobRecordTableViewController"];
        HXApplyItemModel *item6 = [[HXApplyItemModel alloc]initWithIcon:@"apply_6" title:@"即时通讯" destVc:@"HXMyChatViewController"];
        HXApplyItemModel *item7 = [[HXApplyItemModel alloc]initWithIcon:@"apply_7" title:@"签到详情" destVc:@"HXReachTableViewController"];
        
        HXApplyListGroupModel *group3 = [[HXApplyListGroupModel alloc]init];
        group3.items = @[item5,item3,item8,item2,item4,item1,item6,item7];
        [_manageData addObject:group3];
    }
    return _manageData;
}

@end
